﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Utilities
{
    public static class Extensions
    {
        public static string Get_DD_MONTH_YY_DateString(this DateTime dateTime)
        {
            string date = dateTime.Day.ToString();
            string month = Utils.GetMonth(dateTime.Month);
            string year = dateTime.Year.ToString();
            return date + "-" + month + "-" + year;
        }

        public static string GetFileTime(this DateTime dateTime)
        {
            string dateTimeString = dateTime.ToShortDateString();
            dateTimeString = dateTimeString.Replace("/", "-");

            dateTimeString = dateTimeString + " " + string.Format("{0:HH:mm:ss tt}", dateTime).Replace(":", "-");
            
            return dateTimeString;
        }

        public static string FirstLetterUpperCaseRestLowerCase(this string inputString)
        {
            string allLower = inputString.ToLower();
            string firstLetter = allLower.Substring(0, 1);
            firstLetter = firstLetter.ToUpper();
            return firstLetter + allLower.Remove(0, 1);
        }

        public static string GetSquareBracketedCommaSeparatedString(this List<int> integerList)
        {
            List<string> temp = new List<string>();
            integerList.ForEach(t => temp.Add("[" + t.ToString() + "]"));
            return string.Join(",", temp.ToArray());
        }

        public static List<string> GetStringListFromIntegerList(this List<int> integerList)
        {
            List<string> temp = new List<string>();
            integerList.ForEach(t => temp.Add(t.ToString()));
            return temp;
        }

        public static List<int> GetIntegerListFromSquareBracketedCommaSeparatedString(this string input)
        {
            List<int> temp = new List<int>();
            if (string.IsNullOrEmpty(input))
            {
                return temp;
            }
            string[] split = input.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string integerString in split)
            {
                string withoutSquareBrackets = integerString.Replace("[", string.Empty).Replace("]", string.Empty);
                try
                {
                    temp.Add(int.Parse(withoutSquareBrackets));
                }
                catch { }
            }

            return temp;
        }

        public static bool ValidateAmountValue(this string amountAsString, int numberOfDecimalPlaces, out decimal parsedAmount)
        {
            // picking the amount before other things since it is needed for validation
            NumberFormatInfo nfiForAmount = new NumberFormatInfo(); // just two decimal places
            nfiForAmount.NumberDecimalDigits = numberOfDecimalPlaces;
            parsedAmount = 0;
            decimal amountValue;
            try
            {
                decimal.TryParse(amountAsString.Trim(),
                                     NumberStyles.AllowDecimalPoint, nfiForAmount,
                                     out amountValue);
                if (amountValue <= 0)
                {
                    return false;
                }
                else if (Utils.CountDecimalPlaces(amountValue) < 3)
                {
                    parsedAmount = amountValue;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
    }
}
